using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using BlogEngine.Core.Providers.EloqueraProvider.Db;
using Eloquera.Client;
using System.Collections;

namespace BlogEngine.Core.Providers
{
    public partial class EloqueraBlogProvider : BlogProvider
	{
		public override AuthorProfile SelectProfile(string id)
		{

            Parameters param = EqDb.CreateParameters();
            param["id"] = new Guid(id);
            EqProfile eqProfile = (EqProfile)EqDb.ExecuteScalar("select EqProfile where Id = @id");
            if (eqProfile == null)
                return null;
            AuthorProfile profile = new AuthorProfile() {
                DisplayName = eqProfile.DisplayName,
                FirstName = eqProfile.FirstName,
                MiddleName = eqProfile.MiddleName,
                CityTown = eqProfile.CityTown,
                RegionState = eqProfile.RegionState,
                Country = eqProfile.Country,
                Birthday = eqProfile.Birthday,
                AboutMe = eqProfile.AboutMe,
                PhotoURL = eqProfile.PhotoURL,
                Company = eqProfile.Company,
                EmailAddress = eqProfile.EmailAddress,
                PhoneMain = eqProfile.PhoneMain,
                PhoneMobile = eqProfile.PhoneMobile,
                PhoneFax = eqProfile.PhoneFax,
                IsPrivate = eqProfile.IsPrivate
            };
			return profile;
		}

		public override void InsertProfile(AuthorProfile profile)
		{
            Parameters param = EqDb.CreateParameters();
            param["id"] = profile.Id;
            EqProfile eqProfile = (EqProfile)EqDb.ExecuteScalar("select EqProfile where Id = @id");
            if (eqProfile == null) {
                eqProfile = new EqProfile();
                eqProfile.Id = profile.Id;
            }
                eqProfile.DisplayName = profile.DisplayName;
                eqProfile.FirstName = profile.FirstName;
                eqProfile.MiddleName = profile.MiddleName;
                eqProfile.CityTown = profile.CityTown;
                eqProfile.RegionState = profile.RegionState;
                eqProfile.Country = profile.Country;
                eqProfile.Birthday = profile.Birthday;
                eqProfile.AboutMe = profile.AboutMe;
                eqProfile.PhotoURL = profile.PhotoURL;
                eqProfile.Company = profile.Company;
                eqProfile.EmailAddress = profile.EmailAddress;
                eqProfile.PhoneMain = profile.PhoneMain;
                eqProfile.PhoneMobile = profile.PhoneMobile;
                eqProfile.PhoneFax = profile.PhoneFax;
                eqProfile.IsPrivate = profile.IsPrivate;

                EqDb.Store(eqProfile);
		}

		public override void UpdateProfile(AuthorProfile profile)
		{
			InsertProfile(profile);
		}

		public override void DeleteProfile(AuthorProfile profile)
		{
            Parameters param = EqDb.CreateParameters();
            param["id"] = profile.Id;
            EqProfile eqProfile = (EqProfile)EqDb.ExecuteScalar("select EqProfile where Id = @id");
            if (eqProfile != null) {
                EqDb.Delete(eqProfile);
            }

            if (AuthorProfile.Profiles.Contains(profile))
				AuthorProfile.Profiles.Remove(profile);
		}

		public override List<AuthorProfile> FillProfiles()
		{
			List<AuthorProfile> profiles = new List<AuthorProfile>();
            IEnumerable eqProfiles = EqDb.ExecuteQuery("select EqProfile");
            foreach (var profile in eqProfiles) {
                profiles.Add(AuthorProfile.Load(((EqProfile)profile).DisplayName));
            }
			return profiles;
		}
	}
}
